//引入组件库
var nf = sm("do_Notification");
var root = ui("$");
var do_App = sm("do_App");
var do_Page = sm("do_Page");
var hashdata = mm("do_HashData");
var circleUrl = require("url/circle");
var qqHttp = require("helper/http");
var listdata = mm("do_ListData");
var circleHelper = require("helper/circle");
var noData = [{"template":1,"labelContent":"找不到您搜索的圈子纪录"}];
// 本页面-ajax业务请求url
var httpUrl = circleUrl.url.base.search;

// UI
var headerAout = ui("headerAout");
var listView = ui("listView");

listView.bindItems(listdata);

// 定义变量
var page = 1;

// 加载头部
var addheader = headerAout.add("header", "source://view/reuse/searchHeader.ui",
		0, 0);
var textFieldSearch = ui(addheader + ".textFieldSearch");
// andriod返回键
do_Page.on("back", function() {
	do_App.closePage();
});
// 左上角-返回
var headerLeft = ui(addheader + ".headerLeft");
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
	"searchHint" : "搜索圈子"
})
header.refreshData();
headerLeft.on("touch", function() {
	do_App.closePage()
});
var search;
textFieldSearch.on("enter", function(data, e) {
	sendSearch();
});
var sendBtn = ui(addheader + ".sendBtn");
sendBtn.on("touch", function(data, e) {
	sendSearch();
});
function sendSearch(){
	do_Page.hideKeyboard();
	search = textFieldSearch.text;
	if(null!=search&&''!=search){
		loadPage();
	}
	else{
		nf.toast("请输入圈子关键字");
	}
}
function loadPage() {
	page = 1;
	var params = {
		page : page,
		search : search
	};
	qqHttp.post(httpUrl, params, function(data) {
		listView.rebound();
		listdata.removeAll();
		if(data.circlePage.content.length){
			listdata.addData(data.circlePage.content);
		}
		else{
			listdata.addData(noData);
		}
		listView.refreshItems();
	})
}
// 刷新数据
function getNextPageData() {
	page++;
	var params = {
		page : page,
		search : search
	};
	qqHttp.post(httpUrl, params, function(data) {
		listView.rebound();
		listdata.addData(data.circlePage.content);
		listView.refreshItems();
	})
}

// 下拉列表，刷新数据
listView.on("pull", function(data) {
	if (data.state == 2) {
		loadPage();
	}
});

// 上拉列表，翻页数据
listView.on("push", function(data) {
	if (data.state == 2) {
		getNextPageData();
	}
});

//点击一个圈子
listView.on("touch", function(data){
	var onCircle = listdata.getOne(data);
	var circleId = onCircle.id;
	var circleName = onCircle.name;
	circleHelper.toCircleDetail(circleId,circleName);
});